package com.xiaomi.front.service.bill;

import com.xiaomi.front.dao.Bill.BillDao;
import com.xiaomi.front.dao.Describes.DescribesDao;
import com.xiaomi.front.dao.Shopping_cart.ShoppCartDao;
import com.xiaomi.pojo.Bill;
import com.xiaomi.pojo.Billdetailed;
import com.xiaomi.pojo.Commoditys;
import com.xiaomi.pojo.Shoppingcart;
import com.xiaomi.util.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private ShoppCartDao cartDao;
    @Autowired
    private BillDao billDao;
    @Autowired
    private DescribesDao describesDao;

    //生成订单
    @Transactional
    @Override
    public Boolean CreateOrder(int[] scid, double jiage, int aid, int uid, String orderNum) {
        Boolean result = false;

        //1、生成订单
        Bill bill = new Bill();
        bill.setBid(orderNum);
        bill.setBillmoney(jiage);
        bill.setAid(aid);
        bill.setUid(uid);

        int bill_num = billDao.addBill(bill);
        if (bill_num == 1) {
            //2、生成订单详情
            //获取购物车详情
            List<Shoppingcart> billinfo = cartDao.getCarts(scid);

            List<Billdetailed> billdetailedList = new ArrayList<>();
            for (Shoppingcart shoppingCar : billinfo) {
                Billdetailed bd = new Billdetailed();
                bd.setDeid(shoppingCar.getDeid());
                bd.setCprice(shoppingCar.getDepraice());
                bd.setCount(shoppingCar.getCount());
                bd.setBid(bill.getBid());

                billdetailedList.add(bd);
            }
            int details_num = billDao.addBillInfo(billdetailedList);
            if (details_num == billdetailedList.size()) {
                //3.减库存
                int stock_num = 0;
                for (Billdetailed billdetailed : billdetailedList) {
                    int num = describesDao.UpdateStock(billdetailed.getDeid(), billdetailed.getCount());
                    if (num == 1) {
                        stock_num++;
                    }
                }
                if (stock_num == billdetailedList.size()) {
                    //4.删除购物车
                    int car_num = cartDao.DeleteCart(scid);
                    if (car_num == scid.length) {
                        result = true;
                    }
                }
            }
        }
        return result;
    }


    //根据订单编号查详情
    @Override
    public Bill getBillInfo(String orderNum) {
        return billDao.getBillInfo(orderNum);
    }

    //查询订单里商品名称
    @Override
    public List<Commoditys> GetCommName(String orderNum) {
        return billDao.GetCommName(orderNum);
    }

    //修改订单
    @Override
    public int updateBill(int payid, String bid) {
        return billDao.updateBill(payid,bid);
    }

    //根据uid查看订单
    @Override
    public List<Bill> getAll(int uid, int id) {
        return billDao.getAll(uid,id);
    }

    //模糊查询
    @Override
    public List<Bill> getBid(int uid, String bid) {
        return billDao.getBid(uid, bid);
    }

    //查询订单详情
    @Override
    public Bill getOrderNum(int uid, String OrderNum) {
        return billDao.getOrderNum(uid,OrderNum);
    }

    //根据 uid 订单编号 取消订单
    @Override
    public int UpdateBill(int uid, String PrderNum,int sta) {
        return billDao.UpdateBill(uid,PrderNum,sta);
    }

    //根据 uid 状态查看 订单数量
    @Override
    public int getBillCount(int uid, int id) {
        return billDao.getBillCount(uid,id);
    }
}
